import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from Tests.BlogLogin import BlogLogin
from common.TestTool import BlogDriver

class BlogLists:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver
        self.driver.get(self.url)
    def success_to_blogLists(self):
        print(f"当前url: {self.driver.get_current_url()}")
        title = ""
        time_elem = ""
        blog_info = ""
        find_all = ""
        kinds = ""
        try:
            # 等待图片元素加载完成，超时时间设为2秒
            title = WebDriverWait(self.driver.driver, 2).until(
                EC.presence_of_element_located((By.XPATH,'/html/body/div[2]/div[2]/div[1]/div[1]'))
            )
        except Exception as e:
            print(f"未找到标题元素: {e}")
        try:
            time_elem = WebDriverWait(self.driver.driver, 2).until(
                EC.presence_of_element_located((By.XPATH,'/html/body/div[2]/div[2]/div[1]/div[2]'))
            )
        except Exception as e:
            print(f"未找到发表时间元素: {e}")

        try:
            blog_info = WebDriverWait(self.driver.driver, 2).until(
                EC.presence_of_element_located((By.XPATH,'/html/body/div[2]/div[2]/div[1]/div[3]'))
            )
        except Exception as e:
            print(f"未找到github元素: {e}")
        try:
            find_all = WebDriverWait(self.driver.driver, 2).until(
                EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div[1]/a'))
            )
        except Exception as e:
            print(f"未找到查看全文按钮元素: {e}")
        self.driver.log_to_image("success_to_blogLists")
        #点击按钮
        self.driver.click(find_all)
        try:
            WebDriverWait(self.driver.driver, 10).until(EC.url_changes(self.url))
            new_url = self.driver.get_current_url()
            print(f"当前url: {new_url}")
        except Exception as e:
            print(f"URL 未变化或超时: {e}")
    def error_to_bloglists(self):
        BlogLogin().login_to_error()
        self.driver.log_to_image("error_to_bloglists")
        self.driver.get(self.url)
        try:
            WebDriverWait(self.driver.driver, 5).until(EC.url_changes(self.url))
            new_url = self.driver.get_current_url()
            print(f"当前url: {new_url}")
        except Exception as e:
            print(f"URL 未变化或超时: {e}")





